<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<style type="text/css">
	/* #import {
		position: relative;
		top: 1.4px;
	} */
	.center {
		text-align: center;
	}
	.modal-dialog {
		top: 5% ;
	}
	.tree-branch-name .fa-check {
		background-color: #F9A021;border-color: #F9A021;color: #FFF;width: 15px;height: 15px;line-height: 15px;font-size: 11px;text-align: center;border-radius: 3px;position: relative;bottom: 1px;margin-right: 5px
	}
</style>
<div class="page-header">
		<!-- 兼容身份证 -->
		<?= $rmxSchool['school_from'] = ''; ?>
	<h1>
		班级课程表
		<!-- 教师权限不可编辑 -->
		<?php if($rmxUser['user_type'] != USER_TYPE_TEACHER): ?>
			<?php if(empty($schedule)): ?>
				<!-- 同步学校不可添加数据 -->
				<?php if(! $rmxSchool['school_from'] || $rmxSchool['school_from'] == X_DATA_YINTONG || $rmxSchool['school_from'] == X_DATA_CHAOXING || $rmxSchool['school_from'] == X_DATA_DIANXIN): ?>
					<a href="<?= admin_url('schedule/save_schedule') ?>" class="btn btn-info a-custom">
						<i class="ace-icon fa fa-plus bigger-110 "></i> 添加作息时间
					</a>
				<?php endif ?>	
			<?php else: ?>
				<!-- 判断是否年级不同作息 -->
				<?php if($onlyOneSchedule): ?>
					<a href="<?= admin_url('schedule/save_schedule?id=' . $schedule['id']) ?>" class="btn btn-info a-custom">
						<i class="ace-icon fa fa-edit bigger-110 "></i> 修改作息时间
					</a>
				<?php else: ?>
					<a href="<?= admin_url('contents/grade_schedule') ?>" class="btn btn-info a-custom">
						<i class="ace-icon fa fa-edit bigger-110 "></i> 修改作息时间
					</a>
				<?php endif ?>
			<?php endif ?>

			<!-- 同步学校不可添加数据 -->
			<!-- 银通和超星可以添加数据 -->
			<?php if(! $rmxSchool['school_from'] || $rmxSchool['school_from'] == X_DATA_YINTONG || $rmxSchool['school_from'] == X_DATA_CHAOXING || $rmxSchool['school_from'] == X_DATA_DIANXIN): ?>
				<span class="dropdown dropdown-custom">
					<div class="btn btn-info btn-dropdown"  data-toggle="dropdown">
						<i class="ace-icon fa fa-database"></i> 模板导入与导出&nbsp;&nbsp;<i class="ace-icon fa fa-caret-down"></i>
					</div>
					<ul class="dropdown-menu">
						<li>
							<a href="javascript:" id="import">
								<i class="ace-icon fa fa-upload"></i> 导入学校课表数据
							</a>
						</li>
						<li>
							<!-- 当学校还未录入模板时，使用默认的课程模板 -->
							<?php if(empty($schedule) || empty($schedule['schedule_time_json'])): ?>
								<a href="https://resource.rongmixiao.com/2021/08/26/f33e07aeab190d046de6fc11d5513e80.xls" target="_blank">
									<i class="ace-icon fa fa-download"></i> 导出学校课表数据模板
								</a>
							<?php else: ?>
								<a href="<?= admin_url('schedule/export') ?>" target="_blank">
									<i class="ace-icon fa fa-download"></i> 导出学校课表模板
								</a>
							<?php endif ?>
						</li>
					</ul>
				</span>
				<?php if($existTeachingClass): ?>
					<!-- 学校存在教学班 -->
					<span class="dropdown dropdown-custom">
						<div class="btn btn-info btn-dropdown"  data-toggle="dropdown">
							<i class="ace-icon fa fa-database"></i> 走班模板导入与导出&nbsp;&nbsp;<i class="ace-icon fa fa-caret-down"></i>
						</div>
						<ul class="dropdown-menu">
							<li>
								<a href="javascript:" id="import-teaching">
									<i class="ace-icon fa fa-upload"></i> 导入走班课表数据
								</a>
							</li>
							<li>
								<!-- 当学校还未录入模板时，使用默认的课程模板 -->
								<?php if(!empty($schedule) && !empty($schedule['schedule_time_json'])): ?>
									<a href="<?= admin_url('schedule/export_teaching') ?>" target="_blank">
										<i class="ace-icon fa fa-download"></i> 导出走班课表模板
									</a>
								<?php endif ?>
							</li>
						</ul>
					</span>
				<?php endif ?>
				<!-- 暂不开启 -->
				<a class="toggle-grade-schedule btn btn-info a-custom">
					<i class="ace-icon fa fa-edit bigger-110 "></i> 
					<?= $onlyOneSchedule ? '开启年级不同作息' : '关闭年级不同作息'  ?>
				</a>

				<!-- 暂不开启 -->
				<a class="auto-complete-schedule-teacher btn btn-info a-custom">
					<i class="ace-icon fa fa-edit bigger-110 "></i> 
					自动填充任课教师
				</a>
			<?php endif ?>
		<?php endif ?>	
	</h1>
</div>

<!-- 教师权限不可筛选 -->
<?php if($rmxUser['user_type'] != USER_TYPE_TEACHER): ?>
	<div class="col-xs-12 content-filter">
		<div class="widget-box">
			<div class="widget-header">
				<h4 class="widget-title">内容筛选</h4>
				<div class="widget-toolbar">
					<a href="#" data-action="collapse">
						<i class="ace-icon fa fa-chevron-up"></i>
					</a>
					<a href="#" data-action="close">
						<i class="ace-icon fa fa-times"></i>
					</a>
				</div>
			</div>
			<div class="widget-body">
				<div class="widget-main">
					<div class="form-inline" id="data-search">
						<div class="form-inline keyword-search-div">
							<label class="keyword-search-label">年级</label>
							<div class="keyword-search-box">
								<a href="javascript:;" class="keyword-search-keyword keyword-search-grade active" data-id="0">全部</a>
								<?php foreach($gradeList as $grade): ?>
									<a href="javascript:;" class="keyword-search-keyword keyword-search-grade" data-id="<?=$grade['id']?>"><?=$grade['grade_name']?></a>
								<?php endforeach; ?>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
<?php endif ?>	

<div class="col-xs-12">
	<table id="dynamic-table" class="table table-striped table-bordered table-hover" style="margin-top:10px">
		<thead>
			<tr>
				<th width="30%">班级名称</th>
				<th>课程表</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>
				
<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	$(function(){
		// 班级表格
		var table = $('#dynamic-table').dt({
			"ajaxUrl": '<?=admin_url('contents/get_schedule_class_list')?>',
			"columns": [
				{"data": "className"},
				{"data": function(ret){
	            	var action = '';
					if(ret.hasScheduleLesson == 0) {
						// 同步学校不可添加数据
						<?php if(! $rmxSchool['school_from'] || $rmxSchool['school_from'] == X_DATA_YINTONG || $rmxSchool['school_from'] == X_DATA_CHAOXING || $rmxSchool['school_from'] == X_DATA_DIANXIN): ?>
							action += '<a class="green add-class-lesson" href="javascript:" data-classid="' + ret.id + '" data-scheduleid="' + ret.scheduleId + '"><i class="fa fa-plus"></i> 添加</a>';
						<?php else: ?>
							action += '<p class="text-danger">无数据</p>';
						<?php endif ?>
						
					} else {
						action += '<a class="green" href="<?=admin_url('contents/schedule_table')?>/'+ ret.id +'"><i class="fa fa-table"></i> 查看</a> &nbsp;&nbsp;&nbsp;&nbsp;';
						// 同步学校不可添加数据
						<?php if(! $rmxSchool['school_from'] || $rmxSchool['school_from'] == X_DATA_YINTONG || $rmxSchool['school_from'] == X_DATA_CHAOXING || $rmxSchool['school_from'] == X_DATA_DIANXIN): ?>
							// 功能课室的不支持编辑
							if(ret.scheduleId != 0) {
								var url = "<?= admin_url("schedule/save_schedule_lesson?schedule_id=") ?>" + ret.scheduleId + "&class_id=" + ret.id;
								action += '<a class="text-info" href="' + url + '" ><i class="fa fa-edit"></i> 修改</a> &nbsp;&nbsp;&nbsp;&nbsp;';
								action += '<a class="text-danger delete-class-lessons" href="javascript:" data-scheduleid=' + ret.scheduleId + ' data-classid=' + ret.id + ' data-classname="' + ret.className + '" ><i class="fa fa-times"></i> 删除</a>';
							}
						<?php endif ?>
					}
						return action;
					}
	            }
			]
		});
		// 添加班级课程
		$("body").on('click', '.add-class-lesson', function() {
			<?php if(empty($schedule)): ?>
				layer.msg('请先添加课程模板！');
				return;
			<?php endif ?>
			var classId = $(this).data('classid');
			var scheduleId = $(this).data('scheduleid');
			var url = "<?= admin_url("schedule/save_schedule_lesson?schedule_id=") ?>" + scheduleId + "&class_id=" + classId;
			window.location.href = url;
		});
		// 删除班级课程
		$("body").on('click', '.delete-class-lessons', function() {
			var scheduleId = $(this).data('scheduleid');
			var classId = $(this).data('classid');
			var classname = $(this).data('classname');
			var posturl = "<?= admin_url("schedule/delete_class_lessons_action") ?>";
			var data = {
				'schedule_id': scheduleId,
				'class_id': classId
			};
			layer.confirm('确定删除：' + classname + '课程表吗？', { title:"提示", offset: '160px' }, function(){
				ajax_post(posturl, data, function() {
					// 刷新页面
					window.location.href = "<?= admin_url('contents/schedule') ?>";
				});
			});
		});
		// 导入课表
		var upFromurl = '<?=UP_FORM_URL?>';
		var signature = '<?=$signature?>';
		var policy = '<?=$policy?>';
		$("#import").uploader({
			"server": upFromurl,
			"authorization": signature,
			"policy": policy,
			// 只支持上传 xls
			"accept": "xls",
			"callback": function(response) {
				// 上传后的文件名
				var fileLink = upyunUrl + response.url;
				// 导入课表数据
				ajax_post("<?= admin_url('schedule/import') ?>", { fileLink: fileLink}, function(res) {
					// 刷新页面
					location.reload();
				});
			}
		});
		// 导入走班课表
		$("#import-teaching").uploader({
			"server": upFromurl,
			"authorization": signature,
			"policy": policy,
			// 只支持上传 xls
			"accept": "xls",
			"callback": function(response) {
				// 上传后的文件名
				var fileLink = upyunUrl + response.url;
				// 导入课表数据
				ajax_post("<?= admin_url('schedule/import_teaching') ?>", { fileLink: fileLink}, function(res) {
					// 刷新页面
					location.reload();
				});
			}
		});
		// 设置选择文件按钮大小
		setTimeout(function(){
			$(".webuploader-pick").next().width(158).height(32);
		}, 100);
		// 点击关键字
		$("body").on("click", ".keyword-search-keyword", function(){
			var id = $(this).data("id");
			// 高亮当前点击对象
			$(this).parent().find(".keyword-search-keyword").removeClass("active");
			$(this).addClass("active");
			// 筛选年级	
			table.setAjaxData({
				"gradeId": $(this).data('id')
			});
		})

		// 开启不同年级作息
		$("body").on('click', '.toggle-grade-schedule', function() {

			layer.confirm('确定<?= $onlyOneSchedule ? '开启' : '关闭'  ?>年级不同作息？原有的课表数据将失效', { title:"提示", offset: '160px' }, function(){
				var posturl = "<?= admin_url('contents/toggle_grade_schedule_action') ?>";
				ajax_post(posturl, {}, function() {
					// 刷新页面
					window.location.reload();
				});
			});
		});
		// 自动填充任课教师
		$("body").on('click', '.auto-complete-schedule-teacher', function() {
			layer.confirm('确定自动填充任课教师？任课教师为空的课程将自动设置成对应的任课老师', { title:"提示", offset: '160px' }, function(){
				var posturl = "<?= admin_url('contents/auto_complete_schedule_teacher ') ?>";
				ajax_post(posturl, {}, function() {
					// 刷新页面
					window.location.reload();
				});
			});
		});
	})

</script>